package com.wuercloud.http.json.codec;

import com.wuercloud.utils.FastJsonUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.nio.charset.Charset;

/**
 * Created by JiangWL on 2017/2/14.
 */
public abstract class AbstractHttpJsonDecoder<T> extends MessageToMessageDecoder<T> {
    private Class<?> clazz;
    private final static Charset UTF_8 = Charset.forName("UTF-8");


    protected AbstractHttpJsonDecoder(Class<?> clazz) {
        this.clazz = clazz;
    }

    protected Object decode0(ChannelHandlerContext ctx, ByteBuf body) {
        String content = body.toString(UTF_8);

        Object result = FastJsonUtils.jsonToPojo(content, clazz);
        return result;
    }

}
